package com.xindaoapp.happypet.activity;

import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.alipy.Base64;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.utils.ProgressHUD;

public class HelpActivity extends UMBaseActivity {
    @ViewInject(R.id.tittle_common_cancel)
    private ImageView tittle_common_cancel;
    @ViewInject(R.id.tittle_common_content)
    private TextView tittle_common_content;
    @ViewInject(R.id.text)
    private EditText text;
    @ViewInject(R.id.go)
    private Button go;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        ViewUtils.inject(this);
        tittle_common_content.setText("意见反馈");
        tittle_common_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });

        tittle_common_content = (TextView) findViewById(R.id.tittle_common_content);
        go.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(text.getText().toString().trim())) {
                    addHelp(text.getText().toString().trim());
                } else {
                    Toast.makeText(getApplicationContext(), "请输入反馈内容", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private TelephonyManager tm;

    protected void addHelp(String trim) {
        RequestParams params = new RequestParams();
        params.put("phoneType", Base64.encode(Base64.encode("android".getBytes()).getBytes()));
        params.put("systemVersion",
                Base64.encode(Base64.encode(tm.getDeviceSoftwareVersion().getBytes()).getBytes()));
        params.put("phoneSystem", Base64.encode(Base64.encode(Build.MODEL.getBytes()).getBytes()));
        params.put("description", Base64.encode(Base64.encode(trim.getBytes()).getBytes()));
        final ProgressHUD ProgressDialog = ProgressHUD.show(this, "正在提交...", true, true, null);
        AddClient.get("http://ts.leepet.com/plugins/API.v1.0/?&a=other&m=feedBack", params,
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        try {
                            if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                ProgressDialog.dismiss();
                            }
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    finish();
                                }
                            });
                            Toast.makeText(getApplicationContext(), "您的建议已经提交", Toast.LENGTH_SHORT)
                                    .show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        super.onSuccess(statusCode, content);
                    }

                    @Override
                    public void onFailure(Throwable error, String content) {
                        if (null != ProgressDialog && ProgressDialog.isShowing()) {
                            ProgressDialog.dismiss();
                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                finish();
                            }
                        });
                        super.onFailure(error, content);
                    }
                });
    }
}
